package study.仪器解析.串口化通信.生化仪;

import org.springframework.util.ObjectUtils;
import study.utils.SplitUtils;
import study.utils.TimeUnitConversion;

/**
 * @Author 刘伟涛
 * @Date 2024/8/8 9:43
 **/
public class ASTMParser2 {
    public static void main(String[] args) {
        /*
        System.out.println("SpecimenInfo: {");
        System.out.println("}");
        */

        String astmData = "\u0002H|\\^&|||Analyzer|||||Host|||1|20081124151206\r\u000398\r\n" +
                "\u0002P|1||||ZhangDongdong|||M||||||40^Y\r\u000398\r\n" +

                "\u0002O|1|^1^1^10^N|||R|20081124151012|||||||||1||||||||||O\r\u000398\r\n" +
                "\u0002R|1|^^^GGT|4.96|mg/dL|3^12\\0^20|N||F||||20081124151012\r\u000398\r\n" +
                "\u0002Q|1|^1^0^1^N| |ALL||||||||O\r\u000398\r\n" +
                "\u0002L|1|N\r\u000398\r\n";

        ASTMParser2 parser = new ASTMParser2();
        parser.parseASTMMessage(astmData);
    }

    public void parseASTMMessage(String astmData) {
        // 按记录拆分数据
        String[] records = astmData.split("\r\u0003");

        for (String record : records) {
            record = record.replace("\u0002", "").trim();

            if(record.length()<4){
                continue;
            }
            char recordType = record.charAt(4);
            if(!record.contains("\r\n")){
                recordType = record.charAt(0);
            }
            record = record.replace("\r\n", "").trim(); // 去除STX, CRLF

            if (record.isEmpty()) continue;

            // 根据类型进行分类处理

            switch (recordType) {
                case 'H':
                    parseHeaderRecord(record);
                    break;
                case 'P':
                    parsePatientRecord(record);
                    break;
                case 'O':
                    parseOrderRecord(record);
                    break;
                case 'R':
                    parseResultRecord(record);
                    break;
                case 'Q':
                    parseQueryRecord(record);
                    break;
                case 'L':
                    parseTerminationRecord(record);
                    break;
                default:
                    System.out.println("未知记录类型: " + recordType);
            }
        }
    }

    private void parseHeaderRecord(String record) {
        // 示例: H|\^&|||Analyzer|||||Host|||1|20081124151206
        String[] fields = SplitUtils.splitVerticalBar(record);
        System.out.println("Header Record:");
        System.out.println("Sending Application: " + fields[4]);
        System.out.println("Receiving Application: " + fields[9]);
        System.out.println("Date/Time of Message: " + fields[13]);
        System.out.println();
    }

    private void parsePatientRecord(String record) {
        // 示例: P|1||||ZhangDongdong|||M||||||40^Y
        String[] fields = SplitUtils.splitVerticalBar(record);
        System.out.println("Patient Record:");
        System.out.println("Sequence Number: " + fields[1]);
        System.out.println("Patient Name: " + fields[5]);
        System.out.println("Sex: " + fields[8]);
        String[] ageInfo = SplitUtils.splitLatticeSymbol(fields[14]);
        System.out.println("AgeInfo: {");
        System.out.println(" Age: " + ageInfo[0]);
        System.out.println(" AgeUnit: " + TimeUnitConversion.handleDate(ageInfo[1]));
        System.out.println("}");
        System.out.println();
    }

    private void parseOrderRecord(String record) {
        /*样品编号(<样品编号>^<样品编号>^<磁盘编号>^<位置编号>^<稀释剂>)*/
        // 示例: O|1|^1^1^10^N|||R|20081124151012|||||||||1||||||||||O
        String[] fields = SplitUtils.splitVerticalBar(record);
        System.out.println("Order Record:");
        System.out.println("Sequence Number: " + fields[1]);
        String[] specimenInfo = SplitUtils.splitLatticeSymbol(fields[2]);

        System.out.println("SpecimenInfo: {");
        System.out.println(" Sample ID: "+ specimenInfo[0] );
        System.out.println(" Sample No: "+ specimenInfo[1] );
        System.out.println(" Disk ID: "+ specimenInfo[2] );
        System.out.println(" Position No: "+ specimenInfo[3] );
        System.out.println(" Diluent: "+ specimenInfo[4] );
        System.out.println("}");

        System.out.println("instrumentOrSpecimenInfo: {");
        if(!ObjectUtils.isEmpty(fields[3])){
            String[] instrumentOrSpecimenInfo = SplitUtils.splitLatticeSymbol(fields[3]);

            System.out.println(" Qc Name: "+ instrumentOrSpecimenInfo[0] );
            System.out.println(" Qc Lot No: "+ instrumentOrSpecimenInfo[1] );
        }

        System.out.println("}");

        System.out.println("Priority: " + fields[5]);
        System.out.println("Requested/Ordered Date and Time: " + fields[6]);
        System.out.println("Specimen Descriptor: " + fields[15]);
        System.out.println("Report Types: " + fields[25]);
        System.out.println();
    }

    private void parseResultRecord(String record) {
        // 示例: R|1|^^^GGT|4.96|mg/dL|3^12\0^20|N||F||||20081124151012
        String[] fields = SplitUtils.splitVerticalBar(record);
        System.out.println("Result Record:");
        System.out.println("Sequence Number: " + fields[1]);
        String[] universalTestIDInfo = SplitUtils.splitLatticeSymbol(fields[2]);
        System.out.println("Universal Test ID: " + universalTestIDInfo[universalTestIDInfo.length-1]);
        System.out.println("Test Result: " + fields[3] + " " + fields[4]);

        System.out.println("referenceRangesInfo: {");
        String[] referenceRangesInfo = SplitUtils.splitLatticeSymbol(fields[5]);
        if(fields[5].contains("\\")){
            referenceRangesInfo = fields[5].split("\\\\");
            System.out.println(" normal range:" + referenceRangesInfo[0]);
            System.out.println(" critical range:" + referenceRangesInfo[1]);
        }else {
            System.out.println(" target mean:" + referenceRangesInfo[0]);
            System.out.println(" target SD:" + referenceRangesInfo[1]);
        }
        System.out.println("}");

        System.out.println("Result Abnormal Flags: " + fields[6]);
        System.out.println("Nature of Abnormality Testing: " + fields[7]);
        System.out.println("Result Status: " + fields[8]);
        System.out.println("Date/Time Test Completed: " + fields[12]);
        System.out.println();
    }

    private void parseQueryRecord(String record) {
        // 示例: Q|1|^1^0^1^N| |ALL||||||||O
        String[] fields = SplitUtils.splitVerticalBar(record);
        System.out.println("Query Record:");
        System.out.println("Sequence Number: " + fields[1]);

        System.out.println("startingRangeIDInfo: {");
        String[] startingRangeIDInfo = SplitUtils.splitLatticeSymbol(fields[2]);
        System.out.println(" Sample ID: " + startingRangeIDInfo[0]);
        System.out.println(" Sample No: " + startingRangeIDInfo[1]);
        System.out.println(" Disk ID: " + startingRangeIDInfo[2]);
        System.out.println(" Position No: " + startingRangeIDInfo[3]);
        System.out.println(" Diluent: " + startingRangeIDInfo[4]);
        System.out.println("}");

        System.out.println("Universal Test ID: " + fields[4]);
        System.out.println("Request Information Status Codes: " + fields[12]);
        System.out.println();
    }

    private void parseTerminationRecord(String record) {
        // 示例: L|1|N
        String[] fields = SplitUtils.splitVerticalBar(record);
        System.out.println("Termination Record:");
        System.out.println("Sequence Number: " + fields[1]);
        System.out.println("Termination Code: " + fields[2]);
        System.out.println();
    }
}
